namespace ETestIT.Web.Controllers
{
    using System.Web.Routing;
    using Core.Services;
    using System.Web.Mvc;
    using Core;
    using System.Collections.Generic;
    using Extensions;
    using SharpArch.Core.PersistenceSupport;
    using SharpArch.Web.NHibernate;
    using SharpArch.Core;

    [HandleError]
    public class CandidateResumesController : Controller
    {
        public IRepository<JobTitle> JobTitlesRepository { get; set; }

        public CandidateResumesController(
            ICandidateResumeService candidateResumeService, 
            IRepository<JobTitle> jobTitlesRepository,
            IRepository<Candidate> candidateRepository,
            IAppState appState)
        {
            this.candidateResumeService = candidateResumeService;
            this.jobTitlesRepository = jobTitlesRepository;
            this.candidateRepository = candidateRepository;
            this.appState = appState;

            Check.Require(candidateResumeService != null, "candidateResumeService may not be null");
            Check.Require(jobTitlesRepository != null, "jobTitlesRepository may not be null");
            Check.Require(candidateRepository != null, "candidateRepository may not be null");
            Check.Require(appState != null, "appState may not be null");

        }

        [Transaction]
        public ActionResult Index(int id)
        {
            appState.Set(candidateRepository.Get(id));
            
            IList<CandidateResume> candidateResumes = candidateResumeService.GetLatestVersionsResume(id);
            return View(candidateResumes);
        }

        [Transaction]
        public ActionResult Show(int id) {
            var candidateResume = candidateResumeService.Get(id);
            return new ResumeActionResult(candidateResume.TheResume);
        }

        public ActionResult Create() {
            CandidateResumeFormViewModel viewModel = CandidateResumeFormViewModel.CreateCandidateResumeFormViewModel(jobTitlesRepository);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(CandidateResume candidateResume) {
            var candidate = appState.Get<Candidate>();
            
            candidateResume.TheCandidate = candidate;
            candidateResume.TheResume = new Resume();
            
            this.GetResumeFromRequest(candidateResume.TheResume);

            if (candidateResume.IsValid() && candidateResume.TheResume.IsValid())
            {
                candidateResumeService.Create(candidateResume);

                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The candidateResume was successfully created.";

                return RedirectToAction("Index", new RouteValueDictionary(new { id = candidate.Id }));
            }

            CandidateResumeFormViewModel viewModel = CandidateResumeFormViewModel.CreateCandidateResumeFormViewModel(jobTitlesRepository);
            viewModel.CandidateResume = candidateResume;
            return View(viewModel);
        }

        [Transaction]
        public ActionResult Edit(int id) {
            CandidateResumeFormViewModel viewModel = CandidateResumeFormViewModel.CreateCandidateResumeFormViewModel(jobTitlesRepository);
            viewModel.CandidateResume = candidateResumeService.Get(id);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(CandidateResume candidateResume) {
            var candidate = appState.Get<Candidate>();

            candidateResume.TheCandidate = candidate;
            candidateResume.TheResume = new Resume();

            this.GetResumeFromRequest(candidateResume.TheResume);

            CandidateResume candidateResumeToUpdate = candidateResumeService.Get(candidateResume.Id);

            if (candidateResume.IsValid() && candidateResume.TheResume.IsValid())
            {
                {
                    candidateResumeService.Update(candidateResumeToUpdate.TheResume.Id, candidateResume);
                    TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = "The candidateResume was successfully updated.";
                }
                
                return RedirectToAction("Index", new RouteValueDictionary(new { id = candidate.Id }));
            }
            else {
                candidateResumeService.DbContext.RollbackTransaction();

                CandidateResumeFormViewModel viewModel = CandidateResumeFormViewModel.CreateCandidateResumeFormViewModel(jobTitlesRepository);
				viewModel.CandidateResume = candidateResume;
				return View(viewModel);
            }
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            var candidate = appState.Get<Candidate>();

            string resultMessage = "The candidateResume was successfully deleted.";
            CandidateResume candidateResumeToDelete = candidateResumeService.Get(id);

            if (candidateResumeToDelete != null) {
                candidateResumeService.Delete(candidateResumeToDelete.Id);

                try {
                    candidateResumeService.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the candidateResume from being deleted. " +
						"Another item likely depends on this candidateResume.";
                    candidateResumeService.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The candidateResume could not be found for deletion. It may already have been deleted.";
            }

            TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = resultMessage;
            
            return RedirectToAction("Index", new RouteValueDictionary(new { id = candidate.Id }));
        }

		/// <summary>
		/// Holds data to be passed to the CandidateResume form for creates and edits
		/// </summary>
        public class CandidateResumeFormViewModel
        {
            private CandidateResumeFormViewModel() { }

			/// <summary>
			/// Creation method for creating the view model. Services may be passed to the creation 
			/// method to instantiate items such as lists for drop down boxes.
			/// </summary>
            public static CandidateResumeFormViewModel CreateCandidateResumeFormViewModel(IRepository<JobTitle> jobTitlesRepository)
            {
                return new CandidateResumeFormViewModel{
                   JobTitles = jobTitlesRepository.GetAll()
               };
            }

            public CandidateResume CandidateResume { get; internal set; }
            public IList<JobTitle> JobTitles { get; internal set; }
        }

        private readonly ICandidateResumeService candidateResumeService;
        private readonly IRepository<JobTitle> jobTitlesRepository;
        private readonly IRepository<Candidate> candidateRepository;
        private readonly IAppState appState;
    }
}
